package com.yanqu.road.server.cmd.tradewar.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.pb.activity.CrossActivityRankProto;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.*;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_TRADE_WAR_ACTIVITY_RANK, desc = "跨服商战活动积分排行榜")
public class CrossTradeWarRankListCmd extends AbstractCommand {

    private int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CommonActivityProto.CommonActivityRankListReqMsg reqMsg = CommonActivityProto.CommonActivityRankListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

    }

    @Override
    public void sendData(GamePlayer player) {
        CommonActivityProto.CommonActivityRankListRespMsg.Builder respMsg = CommonActivityProto.CommonActivityRankListRespMsg.newBuilder();
        ActivityInfo activityInfo = TradeWarActivityMgr.getOpenActivityInfo(activityId);
        if(null != activityInfo) {
            //   RankProto.RankListMsg.Builder rankListMsg = CrossDataMgr.getCrossRankActivityRankMsg(activityId);
            //    int maxRank;
           /* int conditionType = TradeWarActivityMgr.CONDITION_TYPE_USER;
            maxRank = TradeWarActivityMgr.getMaxRankByType(activityId, conditionType);
            while (null != rankListMsg.getRankListList() && rankListMsg.getRankListCount() > maxRank){
                rankListMsg.removeRankList(rankListMsg.getRankListCount() - 1);
            }
            respMsg.setRankList(rankListMsg);
            respMsg.setMyRank(ActivityMgr.getMyCrossRank(player.getUserId(), rankListMsg));*/

//            CrossActivityRankProto.CrossActivityRankReqMsg.Builder msg = CrossActivityRankProto.CrossActivityRankReqMsg.newBuilder();
//            msg.setActivityId(activityId);
//            msg.setBigRankType(0);
//            player.sendPacket(Protocol.C2_CROSS_GET_TRADE_WAR_ACTIVITY_RANK, msg);

            int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
            if (groupId == -1){
                respMsg.setRankList(RankProto.RankListMsg.newBuilder());
                respMsg.setMyRank(-1);
            }

            YRankProto.YRankReqMsg.Builder reqMsg = YRankProto.YRankReqMsg.newBuilder();
            reqMsg.setActivityId(activityId);
            reqMsg.setGroupId(groupId);
            reqMsg.setIdKey(String.valueOf(eGamePlayerEventType.CrossTradeWarScoreRiseRank.getValue()));
            player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_GET_USER_RANK_LIST, reqMsg, activityId);

            return;
        }else {
            respMsg.setRankList(RankProto.RankListMsg.newBuilder());
            respMsg.setMyRank(-1);
        }
        player.sendPacket(Protocol.U_TRADE_WAR_ACTIVITY_RANK, respMsg);
    }
}
